Telegram Group & Telegram Channel
Sorts one list based on another list containing the desired indexes.

Use zip() and sorted() to combine and sort the two lists, based on the values of indexes.

Use a list comprehension to get the first element of each pair from the result.

Use the reverse parameter in sorted() to sort the dictionary in reverse order, based on the third argument.

CODE:

def sort_by_indexes(lst, indexes, reverse=False):
return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \
x[0], reverse=reverse)]

Example:

a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk']

b = [3, 2, 6, 4, 1, 5]

sort_by_indexes(a, b)

Output:
['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']

sort_by_indexes(a, b, True)

Output:
['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples']

Share and Support
@Python_Codes



tg-me.com/python_codes/179
Create:
Last Update:

Sorts one list based on another list containing the desired indexes.

Use zip() and sorted() to combine and sort the two lists, based on the values of indexes.

Use a list comprehension to get the first element of each pair from the result.

Use the reverse parameter in sorted() to sort the dictionary in reverse order, based on the third argument.

CODE:

def sort_by_indexes(lst, indexes, reverse=False):
return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \
x[0], reverse=reverse)]

Example:

a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk']

b = [3, 2, 6, 4, 1, 5]

sort_by_indexes(a, b)

Output:
['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']

sort_by_indexes(a, b, True)

Output:
['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples']

Share and Support
@Python_Codes

BY Python Codes


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/python_codes/179

View MORE
Open in Telegram


Python Codes Telegram | DID YOU KNOW?

Date: |

A project of our size needs at least a few hundred million dollars per year to keep going,” Mr. Durov wrote in his public channel on Telegram late last year. “While doing that, we will remain independent and stay true to our values, redefining how a tech company should operate.

How To Find Channels On Telegram?

There are multiple ways you can search for Telegram channels. One of the methods is really logical and you should all know it by now. We’re talking about using Telegram’s native search option. Make sure to download Telegram from the official website or update it to the latest version, using this link. Once you’ve installed Telegram, you can simply open the app and use the search bar. Tap on the magnifier icon and search for a channel that might interest you (e.g. Marvel comics). Even though this is the easiest method for searching Telegram channels, it isn’t the best one. This method is limited because it shows you only a couple of results per search.

Python Codes from nl


Telegram Python Codes
FROM USA